class Job
  
  attr_accessor :jobNum, :id, :name, :desc, :nbHash, :nbPass, :algo, :start, :stop, :type, :charset, :keyspace, :keyspacePos, :hashPos, :passLeft
    
  def initialize(jobNum, id, name, desc, algo="md5", salted=false, start=1, stop=14, type=1, charset="abcdefghijklmnopqrstuvwxz")
    @jobNum     = jobNum
    @id         = id        #internal id to handle multi algo submission
    @name       = name
    @desc       = desc
    @algo       = algo
    @start      = start     #char start
    @stop       = stop      #char stop
    @type       = type      #scheduler type
    @startTime  = Time.new()
    @lastTime   = Time.new()
    @nbHash     = 0
    @nbPass     = 0
    @pass       = {}          #hash list
    @saled      = salted    #are we salted
    @charset    = charset
    
    #computing key space
    #FIXME do it only for relevant type
    @keyspace = @charset.length ** stop
    @keyspacePos = 0
    @keyspacePos = @charset.length ** start if start > 1
    puts "creating the job : " + @name + " keyspace " + @keyspace.to_s
  
    @hashPos = 0 #used for keyspace exploration when password are salted
    
  end
  
  #add a hash in the job task if it is not already present return false otherwise.
  def addHash(salt,hash)
      #test if the hash is present
      key = salt + hash
      return false if @pass[key]
      @pass[key] = Pass.new(salt, hash)
      @nbHash   += 1  
      $numHash  += 1  
     # puts "adding hash" + salt + " - " + hash + " to job:" + @name + " id: " + @id.to_s
  end
  
  def addPass(salt, hash, pass)
      @nbPass += 1
      @lastTime = Time.new()
      key = salt + hash
      @pass[key].pass = pass
      $numPass += 1
      if salted || (!salted and @nbPass == 1)
        $keyspace += @keyspace
      end
  end
  
  def getSplit()
    $numSplit += 1
    if salted
      getSplitSalted()
    else
      getSplitUnsalted()
    end
  end
  
  def getSplitSalted()
    #need to deal with password found + need to do with pass indeph
    #when password found think of removing the unit
    
  end

  def getSplitUnsalted(size)
    
    #make sure we don't over do it
    size = @keyspace - @keyspacePos if @keyspacePos + size > @keyspace
    #name and id are used to find the corresponding job -> computing the hash key
    #:jobNum, :id, :name, :desc, :nbHash, :nbPass, :algo, :start, :stop, :type, :charset, :keyspace, :keyspacePos, :hashPos, :passLeft
    #Split.new(@name, @id, @algo, @pass, @type, @keyspacePos, size)
  
  end  
  
  #for how long this job is running
  def duration()
      Time.new() - @startTime
  end
  
  #when has it been updated ?
  def lastUpdate()
    @lastTime
  end
  
  #is the job done
  def isDone()
    false if @keyspacePosition != @keyspace and ( (@salted and @hashPos == @nbHash) or !@salted)
    $jobDone += 1
    $jobNum  -= 1
    true
  end
end